package cn.codergege.kotlinclass.delegatedemo;

public class DelegateDemo1 {
    public static void main(String[] args) {
//        Driver d = new Driver();
//        Manager m = new Manager(d);
        Driver old = new OlderDriver();
        Manager m = new Manager(old);
        m.drive();
        new D(new B()).say(); // 打印 B says ...
        new D(new C()).say(); // 打印 C says ...
    }
}

class Driver {
    public void drive() {
        System.out.println("Driver driving");
    }
}

class Manager {
    private Driver driver; // 建立委托关系
    Manager(Driver driver) {
        this.driver = driver;
    }
    public void drive() {
        driver.drive(); // 经理委托司机开车
    }
}

class OlderDriver extends Driver {
    @Override
    public void drive() {
        System.out.println("老司机开车...");
    }
}

interface Ia {
    void say();
}

class B implements Ia {
    @Override
    public void say() {
        System.out.println("B says ...");
    }
}

class C implements Ia {
    @Override
    public void say() {
        System.out.println("C says ...");
    }
}

class D implements Ia {
    private Ia delegator; // 建立委托关系
    D(Ia ia) {
        this.delegator = ia;
    }
    @Override
    public void say() {
        delegator.say();
    }
}